package com.ruoyi.fztz.zzzecx.service.impl;

import com.ruoyi.fztz.zzzecx.mapper.ZzzeMapper;
import com.ruoyi.fztz.zzzecx.service.IZzzeService;
import com.ruoyi.fztz.zzzecx.vo.ZzzeVo;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 项目归集表Service业务层处理
 *
 * @author huacai
 * @date 2024-11-20
 */
@Service
public class ZzzeServiceImpl implements IZzzeService {
    @Resource
    private ZzzeMapper zzzeMapper;

    @Resource
    private SqlSessionFactory sqlSessionFactory;

    /**
     * 根据条件查询载账总额列表
     *
     * @param zzzeVo
     * @return
     */
    @Override
    public List<ZzzeVo> selectZzZeList(ZzzeVo zzzeVo) {
        String xmName = zzzeVo.getXmName();
        List<String> yearList = zzzeVo.getYearList();

        // 查询结果为 List<Map<String, Object>>
        List<Map<String, Object>> rawResults = zzzeMapper.selectZzZeList(xmName, yearList);

        // 转换为 ZzzeVo 列表
        return rawResults.stream().map(row -> {
            ZzzeVo vo = new ZzzeVo();
            vo.setXmsbId((Integer) row.get("xmsbId"));
            vo.setXmName((String) row.get("xmName"));
            vo.setSsDw((String) row.get("ssDw"));
            vo.setXmBh((String) row.get("xmBh"));
            vo.setKsTime(row.get("ksTime") != null ? row.get("ksTime").toString() : null);
            vo.setJsTime(row.get("jsTime") != null ? row.get("jsTime").toString() : null);
            vo.setXmZe((Double) row.get("xmZe"));
            vo.setLstbId((String) row.get("lstbId"));

            // 动态年份金额映射
            Map<String, Double> yearAmountMap = yearList.stream()
                    .filter(year -> row.containsKey(year + "zzje"))
                    .collect(Collectors.toMap(
                            year -> year,
                            year -> (Double) row.get(year + "zzje")
                    ));
            vo.setYearAmountMap(yearAmountMap);

            return vo;
        }).collect(Collectors.toList());
    }
}
